﻿

Public Class PanelProtection

    'Fields :
    Private _LocalProtection As New Protection
    Private _TargetNode As ZipNode
    Public WriteOnly Property TargetNode As ZipNode
        Set(value As ZipNode)
            _TargetNode = value
            _LocalProtection.CopyFrom(_TargetNode.ZipSettings.Protection)
            ShowProtection()
        End Set
    End Property
    'Show Protection :
    Private Sub ShowProtection()
        KCB_Encryption.SelectedIndex = CInt(_LocalProtection.Encryption)
        KTB_Password.Text = _LocalProtection.Password
        KTB_ConfirmPassword.Text = _LocalProtection.Password
    End Sub
    'Selections :
    Private Sub KCB_Encryption_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles KCB_Encryption.SelectedIndexChanged
        If KCB_Encryption.SelectedIndex = 0 Then
            KTB_Password.Enabled = False
            KTB_ConfirmPassword.Enabled = False
            KTB_Password.Text = Nothing
            KTB_ConfirmPassword.Text = Nothing
            KL_Warning.Visible = False
        Else
            KTB_Password.Enabled = True
            KTB_ConfirmPassword.Enabled = True
            KL_Warning.Visible = True
            KTB_Password.Text = Nothing
            KTB_ConfirmPassword.Text = Nothing
            KL_Warning.Visible = False
        End If
    End Sub
    Private Sub KTB_ConfirmPassword_KeyUp(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles KTB_ConfirmPassword.KeyUp
        If String.IsNullOrWhiteSpace(KTB_Password.Text) = True Then
            ShowWarnings(False, "Edit Some Password")
            KTB_ConfirmPassword.Text = ""
        Else
            If String.Compare(KTB_Password.Text, KTB_ConfirmPassword.Text, False) <> 0 Then
                ShowWarnings(False, "Passwords Not Equivalent")
            Else
                ShowWarnings(True, "Passwords Are Equivalent")
            End If
        End If
    End Sub
    'Show Warnings :
    Private Sub ShowWarnings(ByVal ok As Boolean, ByVal text As String)
        If ok = True Then
            KL_Warning.Values.Image = My.Resources.Img_PasswordOk
        Else
            KL_Warning.Values.Image = My.Resources.Img_PasswordError
        End If
        KL_Warning.Visible = True
        KL_Warning.Text = text
    End Sub
    'Reset :
    Private Sub BSA_ResetEncryption_Click(sender As System.Object, e As System.EventArgs) Handles BSA_ResetEncryption.Click
        KCB_Encryption.SelectedIndex = CInt(Protection.Defaults.Encryption)
    End Sub
    Private Sub BSA_ViewPassword_Click(sender As System.Object, e As System.EventArgs) Handles BSA_ViewPassword.Click
        If BSA_ViewPassword.Checked = ButtonCheckState.Checked Then
            KTB_Password.UseSystemPasswordChar = False
            KTB_Password.PasswordChar = CChar("")
        Else
            KTB_Password.UseSystemPasswordChar = True
        End If
    End Sub
    Private Sub BSA_ViewConfirmPassword_Click(sender As System.Object, e As System.EventArgs) Handles BSA_ViewConfirmPassword.Click
        If BSA_ViewConfirmPassword.Checked = ButtonCheckState.Checked Then
            KTB_ConfirmPassword.UseSystemPasswordChar = False
            KTB_ConfirmPassword.PasswordChar = CChar("")
        Else
            KTB_ConfirmPassword.UseSystemPasswordChar = True
        End If
    End Sub
    'Actions :
    Public Sub Reset()
        _LocalProtection.Reset()
        ShowProtection()
    End Sub
    Public Sub Cancel()
        _LocalProtection.CopyFrom(_TargetNode.ZipSettings.Protection)
        ShowProtection()
    End Sub
    Public Sub Apply()
        If KCB_Encryption.SelectedIndex <> 0 Then
            If String.IsNullOrWhiteSpace(KTB_Password.Text) = True Then
                _TargetNode.ZipSettings.Protection.Encryption = Zip.EncryptionAlgorithm.None
                _TargetNode.ZipSettings.Protection.Password = Nothing
            Else
                If String.Compare(KTB_Password.Text, KTB_ConfirmPassword.Text, False) <> 0 Then
                    _TargetNode.ZipSettings.Protection.Encryption = Zip.EncryptionAlgorithm.None
                    _TargetNode.ZipSettings.Protection.Password = Nothing
                Else
                    _TargetNode.ZipSettings.Protection.Encryption = CType(KCB_Encryption.SelectedIndex, Zip.EncryptionAlgorithm)
                    _TargetNode.ZipSettings.Protection.Password = KTB_Password.Text
                End If
            End If
        Else
            _TargetNode.ZipSettings.Protection.Encryption = Zip.EncryptionAlgorithm.None
            _TargetNode.ZipSettings.Protection.Password = Nothing
        End If
        Me.TargetNode = _TargetNode
    End Sub

End Class
